import { defineStore } from "pinia";
import type { AdminLogin } from "../utils/types";
import { AdminApi } from "../api/index";
import router from "@/router";

export const useUserStore = defineStore("user", {
  state: () => ({
    info: { token: "" },
  }),
  getters: {
    token: (state) => state.info.token,
  },
  actions: {
    async SET_USER(payload: AdminLogin) {
      let res = await AdminApi.login(payload);
      console.log(res);
      if (res.data.code === "200") {
        this.info = res.data.data;
        router.push("/");
      }
    },
  },
  persist: {
    storage: sessionStorage,
    paths: ["info"],
  },
});
